﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace ComicsEditor
{
    public partial class BatchDialog : Form
    {
        private ComicInfo comicInfo;

        public BatchDialog()
        {
            InitializeComponent();
            comboBox1.SelectedIndex = 0;
        }

        public ComicInfo ComicInfo
        {
            set
            {
                comicInfo = value;
                txtFrom.Text = "1";
                txtTo.Text = comicInfo.Count.ToString();
                textBox1.Text = "{num}.jpg";
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            string pattern = textBox1.Text.Trim().Replace("{", "{{").Replace("}", "}}").Replace("{{num}}", "{0}").Replace("{{key}}", "{1}");
            int from = int.Parse(txtFrom.Text) - 1;
            int to = int.Parse(txtTo.Text) - 1;
            for (int i = from; i <= to; i++)
            {
                if (i == comicInfo.Count)
                    (this.Owner as Form1).AddEmptyPage();
                if (i >= 0 && i < comicInfo.Count)
                {
                    string value = String.Format(pattern, i + 1, comicInfo[i].SaveKey != null ? comicInfo[i].SaveKey : comicInfo[i].Key);
                    if (comboBox1.SelectedIndex == 0)
                        comicInfo[i].SaveKey = value;
                    if (comboBox1.SelectedIndex == 1)
                        comicInfo[i].URL = value;
                    if (comicInfo[i].Preview == null)
                        comicInfo.UpdatePreview(comicInfo[i]);
                }
            }
        }
    }
}
